<?php
if (!defined('IN_IPB')) exit("<h1>Incorrect access</h1> You may not access this file without the acp.");

class lottery_loose extends lottery_template
{
	var $name = "Loose Lottery";
	var $description = "Loose Lottery means that if the ticket numbers are 3,5,6,8,10 and the lottery numbers are 10,7,4,7,8 only 2 numbers are matched. Order of numbers does not matter.";
	
	var $settings = array();
	
	function _load(&$lottery)
	{
		parent::_load($lottery);
		
		if($lottery['l_settings'] || $lottery['l_id'] != 0)
		{
			$this->settings = unserialize($lottery['l_settings']);
		}
		else
		{
			$this->settings['balls'] = 3;
			$this->settings['max_number'] = 10;
		}
	}
	
	function admin_form()
	{
		if($this->ipsclass->input['l_id'])
		{
			return "";
		}
		
		$this->ipsclass->adskin->td_header[] = array( "{none}" , "40%" );
		$this->ipsclass->adskin->td_header[] = array( "{none}" , "60%" );
		
		$html .= $this->ipsclass->adskin->start_table("Edit Loose Lottery settings");
		
		$html .= $this->ipsclass->adskin->add_td_row( array("<b>Number of numbers to pick for this lottery</b>",
															  $this->ipsclass->adskin->form_simple_input("balls",$this->settings['balls'])." Numbers"
													 )      );
		
		$html .= $this->ipsclass->adskin->add_td_row( array("<b>The largest number that can be picked</b>",
															  $this->ipsclass->adskin->form_simple_input("max_number",$this->settings['max_number'])
													 )      );
		
		$html .= $this->ipsclass->adskin->end_table();
		
		return $html;
	}
	
	function admin_save($action,&$lottery)
	{
		if($this->ipsclass->input['l_id'])
		{
			return;
		}
		$this->ipsclass->input['balls'] = intval($this->ipsclass->input['balls']);
		$this->ipsclass->input['max_number'] = intval($this->ipsclass->input['max_number']);
		if($this->ipsclass->input['balls'] > $this->ipsclass->input['max_number'])
		{
			$this->ipsclass->admin->error("You need more numbers than balls.");
		}
		if($action == 'hidden')
		{
			$lottery['balls'] = $this->ipsclass->input['balls'];
			$lottery['max_number'] = $this->ipsclass->input['max_number'];
		}
		else
		{
			$lottery['l_settings'] = serialize(array(
														'balls' => $this->ipsclass->input['balls'],
														'max_number' => $this->ipsclass->input['max_number'],
											)		);
		}
	}
	
	function prizes_types($editing = 0)
	{
		if(!$this->prizes)
		{
			$use = $editing && $this->ipsclass->input['l_id'] == 0 ? $this->ipsclass->input : $this->settings;
			$total = $this->ipsclass->numbers->nCr($use['max_number'],$use['balls']);
			for($i = $use['balls']; $i > 0; $i--)
			{
				$this->prizes[$i] = array(
											'display' => $i."/".$use['balls'],
											'matching' => $i."/".$use['balls'],
											'chances' => '1:'.($use['max_number'] == $use['balls'] && $i != $use['balls'] ? '0.00' : $this->ipsclass->numbers->round($this->ipsclass->numbers->divide($total,$this->ipsclass->numbers->multiply($this->ipsclass->numbers->nCr($use['balls'],$i),$this->ipsclass->numbers->nCr($use['max_number']-$use['balls'],$use['balls']-$i))),2)),
										);
			}
		}
		return $this->prizes;
	}
	
	function create_numbers()
	{
		$array = array();
		for($i = 1; $i <= $this->settings['max_number']; $i++)
		{
			$array[$i] = $i;
		}
		
		$rtn = array(0 => array('max_pick' => $this->settings['balls'], 'numbers' => $array));
		return $rtn;
	}
	
	function display_numbers($numbers = array())
	{
		return implode(", ", $numbers[0]);
	}
	
	function check_ticket($ticket, $correct_numbers)
	{
		$numbers = unserialize($ticket['numbers_chosen']);
		$correct = 0;
		foreach(array_keys($numbers[0]) as $k)
		{
			if(isset($correct_numbers[0][$k]))
			{
				$correct++;
			}
		}
		$correct = $correct ? $correct : false;
		return $correct;
	}
}
?>